using UnityEngine;

namespace ZCTouchDrawBoard
{
    /// <summary>
    /// 贴素材的笔
    /// </summary>
    public class StickerPaint : BasePaint
    {
        #region ConstField
        private const string SHADER_NAME = "Hidden/StickerPenShader";
        #endregion

        #region PrivateField
        private int m_StickerIndex;
        #endregion

        #region
        public override PaintAttribute PaintAttribute
        {
            get
            {
                return m_PaintModel;
            }
            set
            {
                if (value == null)
                {
                    return;
                }
                else
                {
                    if (value.MaterialPaths != null && value.MaterialPaths.Length >= 2)
                    {
                        m_StickerIndex = 0;
                        ChangeStickerTextures(value.MaterialPaths);
                        m_PaintModel.Copy(value);
                    }
                }
            }
        }
        #endregion

        #region PublicMethod
        public StickerPaint() : base(SHADER_NAME)
        {

        }

        public StickerPaint(PaintAttribute paintModel) : base(paintModel, SHADER_NAME)
        {

        }
        #endregion

        #region PublicMethod
        public override void DrawToPaper(Vector3[] positions, BasePaperView paper, Mesh mesh = null)
        {
            if (positions == null)
            {
                Logger.w($"{this} position is null.");
                return;
            }

            paper.Use();
            
            Mesh positionMesh = new Mesh();
            Vector3[] vertices = new Vector3[mesh.vertices.Length];
            Vector2[] uv = new Vector2[mesh.uv.Length];
            int[] triangles = new int[mesh.triangles.Length];
            for (int j = 0; j < vertices.Length; j++)
            {
                vertices[j] = mesh.vertices[j];
            }
            for (int j = 0; j < uv.Length; j++)
            {
                uv[j] = mesh.uv[j];
            }
            for (int j = 0; j < triangles.Length; j++)
            {
                triangles[j] = mesh.triangles[j];
            }
            positionMesh.vertices = vertices;
            positionMesh.uv = uv;
            positionMesh.triangles = triangles;

            Matrix4x4 rotateMatrix;
            Vector3[] templateVertices = new Vector3[mesh.vertices.Length];
            for (int i = 0; i < positions.Length; i++)
            {
                if (!CanDrawing(positions[i]))
                    continue;
                m_Shader.SetMatrix("_ProjectMatrix", paper.OrthoMatrix);
                m_Shader.SetTexture("_StickerTex", m_Channels[m_StickerIndex]);
                m_Shader.SetColor("_Color", PaintAttribute.PenColor);
                m_Shader.SetPass(0);
                rotateMatrix = Matrix4x4.Rotate(Quaternion.identity);
                for (int j = 0; j < templateVertices.Length; j++)
                {
                    templateVertices[j] = rotateMatrix * (positions[i] + mesh.vertices[j] * PaintAttribute.PenSize / 2.0f);
                }
                positionMesh.vertices = templateVertices;
                Graphics.DrawMeshNow(positionMesh, Matrix4x4.identity);
                m_StickerIndex = (m_StickerIndex + 1) % PaintAttribute.StickerCount;
            }
            paper.Display();
        }
        #endregion

        #region PrivateMethod
        private void ChangeStickerTextures(string[] materialPaths)
        {
            for (int i = 0; i < materialPaths.Length; i++)
            {
                if (true || m_Channels[i] == null)
                {
                    m_Channels[i] = Resources.Load<Texture2D>(materialPaths[i]);
                }
                else
                {
                    
                }
            }
        }
        #endregion
    }
}
